using System.Xml.Linq;
using X = Adjunct.Framework.Nuspec.XNames.Package.Files.File;

namespace Adjunct.Framework.Nuspec
{
	/// <summary>
	/// 
	/// </summary>
	/// <remarks></remarks>
	public class NuspecInclude
	{
		/// <summary>
		/// Initializes a new instance of the <see cref="NuspecInclude"/> class.
		/// </summary>
		/// <param name="source">The source.</param>
		/// <param name="target">The target.</param>
		/// <param name="exclude">The exclude.</param>
		public NuspecInclude(string source = null, string target = null, string exclude = null)
		{
			Source = source;
			Target = target;
			Exclude = exclude;
		}

		/// <summary>
		/// Gets or sets the exclude.
		/// </summary>
		/// <value>The exclude.</value>
		/// <remarks>Supports * and ** wildcards</remarks>
		public string Exclude { get; set; }

		/// <summary>
		/// Gets or sets the source.
		/// </summary>
		/// <value>The source.</value>
		/// <remarks>Supports * and ** wildcards</remarks>
		public string Source { get; set; }

		/// <summary>
		/// Gets or sets the target.
		/// </summary>
		/// <value>The target.</value>
		/// <remarks></remarks>
		public string Target { get; set; }

		/// <summary>
		/// Clones this instance.
		/// </summary>
		/// <returns></returns>
		/// <remarks></remarks>
		public NuspecInclude Clone()
		{
			return new NuspecInclude(Source, Target, Exclude);
		}

		/// <summary>
		/// Performs an explicit conversion from <see cref="Adjunct.Framework.Nuspec.NuspecInclude"/> to 
		/// <see cref="System.Xml.Linq.XElement"/>.
		/// </summary>
		/// <param name="include">The include.</param>
		/// <returns>
		/// The result of the conversion.
		/// </returns>
		public static explicit operator XElement(NuspecInclude include)
		{
			return (include == null)
			       	? null
			       	: new XElement(X.ElementName,
			       	               include.Source.ToXAttribute(X.Source),
			       	               include.Target.ToXAttributeIfNotEmpty(X.Target),
			       	               include.Exclude.ToXAttributeIfNotEmpty(X.Exclude));
		}

		/// <summary>
		/// Performs an explicit conversion from <see cref="System.Xml.Linq.XElement"/> to 
		/// <see cref="Adjunct.Framework.Nuspec.NuspecInclude"/>.
		/// </summary>
		/// <param name="element">The element.</param>
		/// <returns>
		/// The result of the conversion.
		/// </returns>
		public static explicit operator NuspecInclude(XElement element)
		{
			if (element == null)
			{
				return null;
			}
			element.EnsureElementNameIs(X.ElementName);
			return new NuspecInclude(element.AttributeValue(X.Source),
			                         element.AttributeValue(X.Target, null),
			                         element.AttributeValue(X.Exclude, null));
		}
	}
}